#sys.path.append("..")
from TryStore import TryStore
import RPi.GPIO as GPIO
import time
from adafruit_servokit import ServoKit

#set servo 16 channnels controller
kit = ServoKit(channels=16)
#set GPIO 17 for detect the audio reciver
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.IN)
GPIO.add_event_detect(channel,GPIO.BOTH,bouncetime=300)
#GPIO.add_event_callback(channel,callback)

#def callback(channel):
good = GPIO.input(channel)
#interact with people
print("please choose modle between tryout or use and type it")
modle = input("enter tryout/use:")
if modle == "tryout":
    info = True
if modle == "use":
    info = False
#interact with people and choose try or use
if info:
    #try thing you want
    a = 6 
    while good==1 :
        #good = GPIO.input(channel)
        kit.servo[8].angle = 10 + a
        time.sleep(1)
        kit.servo[0].angle = 30
        time.sleep(1)
        kit.servo[0].angle = 70
        time.sleep(1)
        kit.servo[0].angle = 0
        a += 15
        print("please tell me is that ok?")
        tellit = input("enter y/n:")
        if tellit == "y":
            good = 0
        if tellit == "n":
            good = 1
        #time.sleep(0.5)
        #good = GPIO.input(channel)
    else:
        TM = TryStore()
        number = 10 + a
        codeT = "kit.servo[8].angle = {}".format(number)
        TM.update_code(codeT)
        kit.servo[8].angle = 10
        print("it is tryed end and stored")

else:
    TS = TryStore()
    position = "first"
    codeT = TS.use_code(position)
    #print(codeT)
    #typ = type(codeT)
    #print(typ)
    stringone = str(codeT)
    stringtwo = stringone.replace('(','')
    stringthree = stringtwo.replace(')','')
    stringfour = stringthree.replace(',','')
    stringfive = stringfour.replace("'","")
    #print(stringfour)
    #stringcode = 'kit.servo[8].angle=20'
    exec(stringfive)
    #kit.servo[8].angle = 10
    print(stringfive)
    time.sleep(1)
    kit.servo[0].angle = 30
    time.sleep(1)
    kit.servo[0].angle = 70
    time.sleep(1)
    kit.servo[8].angle = 1
    time.sleep(2)
    kit.servo[0].angle = 0
